package str33_strumienie;

import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Employee {
	private String name;
	private double salary;
	private Date hireDate;
	
	public Employee() {}

	public Employee(String name, double salary, int year, int month, int day) {
		this.name = name;
		this.salary = salary;
		GregorianCalendar calendar = new GregorianCalendar(year, month-1, day);
		this.hireDate = calendar.getTime();
	}

	public String getName() {
		return name;
	}

	public double getSalary() {
		return salary;
	}

	public Date getHireDate() {
		return hireDate;
	}
	
	public void raisesalary(double byPercent) {
		double raise = salary * byPercent / 100;
		salary += raise;
	}

	@Override
	public String toString() {
		return getClass().getSimpleName() + 
				"[name=" + name + 
				",salary=" + salary + 
				",hireDate=" + hireDate + "]";
	}
	
	/**
	 * Zapisuje dane obiektu klasy Employee
	 * do obiektu klasy PrintWriter
	 * @param out obiekt klasy PrintWriter
	 */
	public void writeData(PrintWriter out) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(hireDate);
		out.println(name + "|" +
				salary + "|" + 
				calendar.get(Calendar.YEAR) + "|" +
				calendar.get(Calendar.MONTH) + "|" +
				calendar.get(Calendar.DAY_OF_MONTH));
	}
	
	/**
	 * Wczytuje dane obiektu klasy Employee
	 * @param in obiekt klasy Scanner
	 */
	public void readData(Scanner in) {
		String line = in.nextLine();
		String[] tokens = line.split("\\|");
		name = tokens[0];
		salary = Double.parseDouble(tokens[1]);
		int y = Integer.parseInt(tokens[2]);
		int m = Integer.parseInt(tokens[3]);
		int d = Integer.parseInt(tokens[4]);
		GregorianCalendar calendar = new GregorianCalendar(y, m-1, d);
		hireDate = calendar.getTime();
	}
}
